//
//  FKMineVC1.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/15.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKMineVC1.h"
#import "FKCustomBtn.h"
#import "FKOrderVC.h"
#import "FKMyInfoVC.h"
#import "FKMyBalanceVC.h"
#import "FKMyCollectVC.h"
#import "FKMyCouponVC.h"
#import <UShareUI/UShareUI.h>
#import "FKShareVC.h"

@interface FKMineVC1 ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *avatarTop;
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *balanceLabel;
@property (weak, nonatomic) IBOutlet UILabel *cartLabel;
@property (weak, nonatomic) IBOutlet UILabel *couponLabel;
@property (weak, nonatomic) IBOutlet UIImageView *bottomImageView;

@property (weak, nonatomic) IBOutlet FKCustomBtn *waitPayBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *noUseBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *completeBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *afterSaleBtn;

@property (weak, nonatomic) IBOutlet FKCustomBtn *logisticsBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *myCollectBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *addressBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *inviteCodeBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *serviceBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *feedbackBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *consociationBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *settingBtn;


@property (nonatomic, copy) NSString *serviceTel;


@end

@implementation FKMineVC1

- (instancetype)init
{
    return [UIStoryboard storyboardWithName:@"FKMineVC1" bundle:nil].instantiateInitialViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *header = self.tableView.tableHeaderView;
    header.fk_height = fk_adjustW(180) + fkOffsetTop;
    self.tableView.tableHeaderView = header;
    self.avatarTop.constant = fk_adjustW(42) + fkOffsetTop;
    
    [self.balanceLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topItemDidTap:)]];
    [self.cartLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topItemDidTap:)]];
    [self.couponLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topItemDidTap:)]];
    [self.bottomImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bottomImageDidTap)]];
    
    self.waitPayBtn.imagePosition = FKImagePositionUp;
    self.noUseBtn.imagePosition = FKImagePositionUp;
    self.completeBtn.imagePosition = FKImagePositionUp;
    self.afterSaleBtn.imagePosition = FKImagePositionUp;
    
    self.logisticsBtn.imagePosition = FKImagePositionUp;
    self.myCollectBtn.imagePosition = FKImagePositionUp;
    self.addressBtn.imagePosition = FKImagePositionUp;
    self.inviteCodeBtn.imagePosition = FKImagePositionUp;
    self.serviceBtn.imagePosition = FKImagePositionUp;
    self.feedbackBtn.imagePosition = FKImagePositionUp;
    self.consociationBtn.imagePosition = FKImagePositionUp;
    self.settingBtn.imagePosition = FKImagePositionUp;
    
    [self setupBadge:self.waitPayBtn];
    [self setupBadge:self.noUseBtn];
    [self setupBadge:self.completeBtn];
    [self setupBadge:self.afterSaleBtn];
//    [self setupBadge:self.logisticsBtn];
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, fkOffsetBottom + fkTabBarH, 0);
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self getServicePhone];
}

- (void)setupBadge:(UIButton *)btn
{
    UIView *redView = [[UIView alloc] init];
    [btn addSubview:redView];
    redView.backgroundColor = [UIColor redColor];
    redView.tag = 3;
    [redView fk_viewCornerRadius:fk_adjustW(4) borderWidth:0 borderColor:nil];
    
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(8));
        make.top.offset(10);
        make.right.offset(-12);
    }];
}

- (void)updateBadge:(UIButton *)btn show:(BOOL)show
{
    UIView *redView = [btn viewWithTag:3];
    redView.hidden = !show;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([FKCacheTool isLogin]) {
        [self loadData];
    } else {
        self.nameLabel.text = @"登录/注册";
        self.avatarView.image = kDefaultAvatar;
        self.balanceLabel.text = @"--\n余额";
        self.balanceLabel.attributedText = [self.balanceLabel.text fk_addAttributeStyle:@"余额" color:[UIColor whiteColor] font:fk_adjustFont(12)];
        self.cartLabel.text = @"--\n购物车";
        self.cartLabel.attributedText = [self.cartLabel.text fk_addAttributeStyle:@"购物车" color:[UIColor whiteColor] font:fk_adjustFont(12)];
        self.couponLabel.text = @"--\n优惠券";
        self.couponLabel.attributedText = [self.couponLabel.text fk_addAttributeStyle:@"优惠券" color:[UIColor whiteColor] font:fk_adjustFont(12)];
    }
}

/** 点击进入个人中心 */
- (IBAction)userInfoBtnClicked:(UIButton *)sender
{
    fkWeakSelf(self);
    [FKLoginTool checkLogin:^{
        FKMyInfoVC *vc = [[FKMyInfoVC alloc] init];
        [weakself.navigationController pushViewController:vc animated:true];
    }];
}

- (void)topItemDidTap:(UITapGestureRecognizer *)gesture
{
    UILabel *label = (UILabel *)gesture.view;
    if (label.tag == 10) {
        FKMyBalanceVC *vc = [[FKMyBalanceVC alloc] init];
        fkPushNav(vc);
    } else if (label.tag == 11) {
        fkPushNav([[NSClassFromString(@"FKShoppingCartVC") alloc] init]);
    } else if (label.tag == 12) {
        FKMyCouponVC *vc = [[FKMyCouponVC alloc] init];
        fkPushNav(vc);
    }
}

- (IBAction)allOrderBtnClicked:(UIButton *)sender
{
    FKOrderVC *vc = [[FKOrderVC alloc] init];
    vc.type = @"1";
    fkPushNav(vc);
}

- (IBAction)itemBtnClicked:(FKCustomBtn *)sender
{
    NSString *title = sender.titleLabel.text;
//    @[@"全部", @"待付款", @"待发货", @"待收货", @"售后"];
//    @[@"", @"10", @"20", @"30", @"50"];
    if ([title isEqualToString:@"待付款"]) {
        FKOrderVC *vc = [[FKOrderVC alloc] init];
        vc.type = @"1";
        vc.status = @"10";
        fkPushNav(vc);
    } else if ([title isEqualToString:@"待消费"]) {
        FKOrderVC *vc = [[FKOrderVC alloc] init];
        vc.type = @"1";
        vc.status = @"20";
        fkPushNav(vc);
    } else if ([title isEqualToString:@"已完成"]) {
        FKOrderVC *vc = [[FKOrderVC alloc] init];
        vc.type = @"1";
        vc.status = @"40";
        fkPushNav(vc);
    } else if ([title isEqualToString:@"售后"]) {
        FKOrderVC *vc = [[FKOrderVC alloc] init];
        vc.type = @"1";
        vc.status = @"50";
        fkPushNav(vc);
    } else if ([title isEqualToString:@"物流订单"]) {
        FKOrderVC *vc = [[FKOrderVC alloc] init];
        vc.type = @"2";
        vc.title = title;
        fkPushNav(vc);
    } else if ([title isEqualToString:@"我的收藏"]) {
        fkPushNav([[NSClassFromString(@"FKMyCollectVC") alloc] init]);
    }  else if ([title isEqualToString:@"收货地址"]) {
        fkPushNav([[NSClassFromString(@"FKAddressVC") alloc] init]);
    } else if ([title isEqualToString:@"填邀请码"]) {
        fkPushNav([[NSClassFromString(@"FKInviteCodeVC") alloc] init]);
    } else if ([title isEqualToString:@"邀请人信息"]) {
        fkPushNav([[NSClassFromString(@"FKInviteInfoVC") alloc] init]);
    } else if ([title isEqualToString:@"联系客服"]) {
        if (self.serviceTel) {
            fk_call(self.serviceTel);
        }
    } else if ([title isEqualToString:@"意见反馈"]) {
        fkPushNav([[NSClassFromString(@"FKFeedbackVC") alloc] init]);
    } else if ([title isEqualToString:@"商家合作"]) {
        fkPushNav([[NSClassFromString(@"FKBusinessCooperationVC") alloc] init]);
    } else if ([title isEqualToString:@"设置"]) {
        fkPushNav([[NSClassFromString(@"FKMySettingVC") alloc] init]);
    }
}

- (void)bottomImageDidTap
{
    FKShareVC *vc = [[FKShareVC alloc] init];
    fkPushNav(vc);
}

#pragma mark - UITableViewDelegate
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - 网络请求
- (void)loadData
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=person_info" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            // 缓存用户信息
            NSDictionary *userInfo = result.data;
            [FKCacheTool saveUserInfo:userInfo];
            
            NSString *level = userInfo[@"level"];
            if (level && ![level isEqualToString:@"10"]) {
                self.iconView.image = [UIImage imageNamed:@"huiyuanyonghu"];
            }
            
            [self.avatarView sd_setImageWithURL:[NSURL URLWithString:userInfo[@"avatar"]] placeholderImage:kDefaultAvatar];
            self.nameLabel.text = userInfo[@"nickname"];
            self.balanceLabel.text = fkFORMAT(@"%@\n余额", userInfo[@"balance"]);
            self.balanceLabel.attributedText = [self.balanceLabel.text fk_addAttributeStyle:@"余额" color:[UIColor whiteColor] font:fk_adjustFont(12)];
            self.cartLabel.text = fkFORMAT(@"%@\n购物车", userInfo[@"cart_num"]);
            self.cartLabel.attributedText = [self.cartLabel.text fk_addAttributeStyle:@"购物车" color:[UIColor whiteColor] font:fk_adjustFont(12)];
            self.couponLabel.text = fkFORMAT(@"%@\n优惠券", userInfo[@"coupon_count"]);
            self.couponLabel.attributedText = [self.couponLabel.text fk_addAttributeStyle:@"优惠券" color:[UIColor whiteColor] font:fk_adjustFont(12)];
            
            if ([fkFORMAT(@"%@", userInfo[@"is_inviter"]) isEqualToString:@"1"]) { // 已经填写邀请码
                [self.inviteCodeBtn setTitle:@"邀请人信息" forState:UIControlStateNormal];
            } else {
                [self.inviteCodeBtn setTitle:@"填邀请码" forState:UIControlStateNormal];
            }
            
            
            [self updateBadge:self.waitPayBtn show:[userInfo[@"order_counta"] boolValue]];
            [self updateBadge:self.noUseBtn show:[userInfo[@"order_countb"] boolValue]];
            [self updateBadge:self.completeBtn show:[userInfo[@"order_countc"] boolValue]];
            [self updateBadge:self.afterSaleBtn show:[userInfo[@"order_countd"] boolValue]];
        }
    } failure:nil];
}

- (void)getServicePhone
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=kf_tel" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            self.serviceTel = result.data[@"tel"];
        }
    } failure:nil];
}




@end
